package com.rizzo.analytics.domain.performance;

import com.rizzo.infrastructure.web.validator.Save;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.Collection;

public class MonitoredService implements Serializable {

	public static final int INSTANCE_NAME_MAX_LENGTH = 30;
	public static final int INSTANCE_INDEX_MAX_LENGTH = 6;
	public static final int SERVICE_MAX_LENGTH = 300;

	private static final long serialVersionUID = 7L;

	@NotNull(groups = {Save.class}, message = "performance.monitored.service.instancename.field.empty")
	@Size(min = 1, max = INSTANCE_NAME_MAX_LENGTH, message = "performance.monitored.service.instancename.field.max.length", groups = {Save.class})
	private String instanceName;

	@NotNull(groups = {Save.class}, message = "performance.monitored.service.instanceindex.field.empty")
	@Size(min = 1, max = INSTANCE_INDEX_MAX_LENGTH, message = "performance.monitored.service.instanceindex.field.max.length", groups = {Save.class})
	private String instanceIndex;

	@NotNull(groups = {Save.class}, message = "performance.monitored.service.service.field.empty")
	@Size(min = 1, max = SERVICE_MAX_LENGTH, message = "performance.monitored.service.service.field.max.length", groups = {Save.class})
	private String service;

	private long totalCalls;

	@Valid
	@Size(min = 1, message = "performance.monitored.service.calls.empty", groups = {Save.class})
	private Collection<CallInfo> calls;

	public MonitoredService() {
	}

	public MonitoredService(String service, String instanceName, String instanceIndex, long totalCalls, Collection<CallInfo> calls) {
		this.service = service;
		this.instanceName = instanceName;
		this.instanceIndex = instanceIndex;
		this.totalCalls = totalCalls;
		this.calls = calls;
	}

	public String getService() {
		return service;
	}

	public void setService(String service) {
		this.service = service;
	}

	public long getTotalCalls() {
		return totalCalls;
	}

	public void setTotalCalls(long totalCalls) {
		this.totalCalls = totalCalls;
	}

	public Collection<CallInfo> getCalls() {
		return calls;
	}

	public void setCalls(Collection<CallInfo> calls) {
		this.calls = calls;
	}

	public String getInstanceName() {
		return instanceName;
	}

	public void setInstanceName(String instanceName) {
		this.instanceName = instanceName;
	}

	public String getInstanceIndex() {
		return instanceIndex;
	}

	public void setInstanceIndex(String instanceIndex) {
		this.instanceIndex = instanceIndex;
	}

	public void addCallTotal(long callTotal) {
		this.totalCalls += callTotal;
	}

	@Override
	public boolean equals(Object o) {
		return EqualsBuilder.reflectionEquals(this, o);
	}

	@Override
	public int hashCode() {
		return HashCodeBuilder.reflectionHashCode(this);
	}

	@Override
	public String toString() {
		return new ToStringBuilder(this).
				append("calls", calls).
				append("instanceName", instanceName).
				append("instanceIndex", instanceIndex).
				append("service", service).
				append("totalCalls", totalCalls).
				toString();
	}
}
